package com.minglead.springboot.platform.sys.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.minglead.springboot.common.config.Global;
import com.minglead.springboot.common.crud.BaseController;
import com.minglead.springboot.common.persistence.Page;
import com.minglead.springboot.common.utils.StringUtils;
import com.minglead.springboot.platform.sys.entity.DesktopPlug;
import com.minglead.springboot.platform.sys.service.DesktopPlugService;
import com.minglead.springboot.platform.sys.utils.ActionResponse;
/**
 * 桌面插件Controller
 * @author clz
 * @version 2017-09-05
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/desktopPlug")
public class DesktopPlugController extends BaseController {

	@Autowired
	private DesktopPlugService desktopPlugService;
	
	@ModelAttribute
	public DesktopPlug get(@RequestParam(required=false) String id) {
		DesktopPlug entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = desktopPlugService.get(id);
		}
		if (entity == null){
			entity = new DesktopPlug();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:desktopPlug:view")
	@RequestMapping(value = {"list", ""})
	public String list(DesktopPlug desktopPlug, HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/sys/desktopPlugList";
	}

	@RequiresPermissions("sys:desktopPlug:view")
	@RequestMapping(value = "form")
	public String form(DesktopPlug desktopPlug, Model model) {
		model.addAttribute("desktopPlug", desktopPlug);
		return "modules/sys/desktopPlugForm";
	}

	@RequiresPermissions("sys:desktopPlug:edit")
	@RequestMapping(value = "save")
	public String save(DesktopPlug desktopPlug, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, desktopPlug)){
			return form(desktopPlug, model);
		}
		desktopPlugService.save(desktopPlug);
		addMessage(redirectAttributes, "保存桌面插件成功");
		return "redirect:"+Global.getAdminPath()+"/sys/desktopPlug/?repage";
	}
	
	
	@RequiresPermissions("sys:desktopPlug:edit")
	@RequestMapping(value = "delete")
	public void delete(@Param("id") String id, HttpServletResponse response,RedirectAttributes redirectAttributes) {		
		ActionResponse r = new ActionResponse(1,"操作成功！");
		if(StringUtils.isEmpty(id)){
			r.setMessage("删除失败,id不能为null");
			this.responseObject(r, response);
			return;
		}	
		String[] _id = id.split(",");
		for(String id0:_id){
			DesktopPlug temp = new DesktopPlug();
			temp.setId(id0);
			desktopPlugService.delete(temp);
		}	
		this.responseObject(r, response);
	}
	
	@RequiresPermissions("sys:desktopPlug:view")
	@RequestMapping(value="loadData")
	public void loadData(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String name = request.getParameter("name");
		DesktopPlug entity=new DesktopPlug();
		entity.setName(name);
        Page<DesktopPlug> page = desktopPlugService.findPage(new Page<DesktopPlug>(request, response),entity); 
        List<DesktopPlug> desktopPlugList = page.getList();
        for(DesktopPlug d:desktopPlugList){
            desktopPlugService.wrapEntity(d);
        }

		renderGrid(request, response, page);
	}
	
	
	

}